#include <iostream>


class LinearQueue {
private:
	int* arr;
	int size;
	//队头指针
	int front;
	//队尾指针
	int rear;

public:
	LinearQueue(int capacity) {
		size = capacity;
		arr = new int[size];
		front = -1;
		rear = -1;
	}

	~LinearQueue() {
		delete[] arr;
	}


public:

	bool Enqueue(int val) {
		if (rear == size - 1) {
			return false;
		}

		rear++;
		arr[rear] = val;
		return true;
	}

	int Dequeue() {
		if (IsEmpty()) {
			throw -1;
		}

		front++;
		int result = arr[front];
		//队列中只有一个元素，出队后要重置指针
		if (front == rear) {
			front = -1;
			rear = -1;
		}
		return result;
	}


	bool IsEmpty() const {
		return front == rear;
	}

	//看一眼队首的元素
	int Peek() {
		if (IsEmpty()) {
			throw -1;
		}
		int head = front + 1;
		return arr[head];
	}


	void Print() {
		if (!IsEmpty()) {
			for (int i = front + 1; i <= rear; i++) {
				std::cout << arr[i] << " ";
			}
			std::cout << std::endl;
		}
	}

};

/*

用数组实现队列

缺点
1:rear顶到头了的话，就再也加不进元素了

2：front前面的空间被浪费了
*/
int main() {
	
	LinearQueue q(5);

	q.Enqueue(1);
	q.Enqueue(2);
	q.Enqueue(3);
	q.Enqueue(4);
	q.Enqueue(5);

	//while(!q.IsEmpty()) {
	//	std::cout <<q.Dequeue() << std::endl;
	//}

	std::cout << q.Dequeue() << std::endl;
	std::cout << q.Dequeue() << std::endl;
	std::cout << q.Dequeue() << std::endl;
	std::cout << q.Dequeue() << std::endl;






	return 0;
}